package com.ymatou.iapi.refund.testcase;

import java.util.List;

import net.sf.json.JSONObject;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.enums.CancelOrderTypeEnum;
import com.ymatou.iapi.optrade.resp.Order;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.refund.parameter.AccepterTypeEnum;
import com.ymatou.iapi.refund.parameter.CreateReturnBillBean;
import com.ymatou.iapi.refund.parameter.GetBuyerReturnInfoByOrderIdBean;
import com.ymatou.iapi.refund.parameter.CreateReturnBillResp.ReturnBillList;
import com.ymatou.iapi.refund.parameter.RefundStatusEnum;
import com.ymatou.iapi.refund.parameter.ReturnBillInfoList;
import com.ymatou.iapi.refund.parameter.ReturnBillInfoResponse;
import com.ymatou.iapi.refund.service.CreateReturnBillCall;
import com.ymatou.iapi.refund.service.GetBuyerReturnInfoByOrderIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.RefundCallService;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;
import com.ymttest.database.model.CS_ReturnApplyBill;
import com.ymttest.database.model.CS_ReturnApplyProducts;
import com.ymttest.database.sqlwapper.CS_ReturnApplyBillWapper;
import com.ymttest.database.sqlwapper.CS_ReturnApplyProductsWapper;
/**
*根据订单Id批量获取订单信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetBuyerReturnInfoByOrderId{
	private static GetBuyerReturnInfoByOrderIdBean bean;
	private static GetBuyerReturnInfoByOrderIdCall call;
	private static CreateReturnBillCall createCall;
    private static CreateReturnBillBean createBean;
    private static CS_ReturnApplyBillWapper returnApplyBillWapper=new CS_ReturnApplyBillWapper();
	private static CS_ReturnApplyProductsWapper returnApplyProducts=new CS_ReturnApplyProductsWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据订单Id批量获取订单信息");
	}

	@Before
	public void caseUp() {
		bean=new GetBuyerReturnInfoByOrderIdBean();
		call=new GetBuyerReturnInfoByOrderIdCall();
		createCall=new CreateReturnBillCall();
		createBean=new CreateReturnBillBean();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBuyerReturnInfoByOrderId_001() {
		Logger.start(true,"正例-获取买家订单的退单信息");
		try {
			//创建一个订单
			TradingPlaceOrderCallService tradingService = new TradingPlaceOrderCallService();
			RefundCallService service = new RefundCallService();
			TradingServiceResp tradingServiceResp = tradingService
					.placeOrderWithLuxury(false);
			
			createBean=service.setCreateReturnBillBean_AcceptOrder(tradingServiceResp,tradingService,false);
			createCall=service.callCreateReturnBill(createBean,createCall);
			bean.setOrderId(createBean.getOrderId());
			bean.setUserId(createBean.getUserid());
			call.setData(bean);
			call.callService();	
			//退货状态：卖家发起   接受类型：洋管家
			verifyReturn(bean,call,1,2);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBuyerReturnInfoByOrderId_002() {
		Logger.start(true,"正例-获取买家订单的退单信息-订单不接单，直接取消");
		try {
			//创建一个订单
			TradingPlaceOrderCallService tradingService = new TradingPlaceOrderCallService();
			RefundCallService service = new RefundCallService();
			TradingServiceResp tradingServiceResp = tradingService
					.placeOrderWithLuxury(false);
			
			createBean=service.setCreateReturnBillBean(tradingServiceResp,tradingService);
			createCall=service.callCreateReturnBill(createBean,createCall);
			bean.setOrderId(createBean.getOrderId());
			bean.setUserId(createBean.getUserid());
			call.setData(bean);
			call.callService();	
			//退货状态：卖家发起   接受类型：买家
			verifyReturn(bean,call,20,1);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBuyerReturnInfoByOrderId_003() {
		Logger.start(true,"正例-获取买家订单的退单信息-主动取消");
		try {
			//创建一个订单
			TradingPlaceOrderCallService tradingService = new TradingPlaceOrderCallService();
			RefundCallService service = new RefundCallService();
			TradingServiceResp tradingServiceResp = tradingService
					.placeOrderWithLuxury(false);
			
			List<Order> orders=tradingServiceResp.getOrderList();
            long orderId=orders.get(0).getOrderId();
            long userId=tradingServiceResp.getUserId();
			createBean=service.setCreateReturnBillBean(tradingServiceResp,tradingService);
			createCall=service.callCreateReturnBill(createBean,createCall);
			
			//取消订单
			TradingCallService.cancelOrder(orderId,CancelOrderTypeEnum.BUYER_CANCEL_ORDER,userId);
            
			bean.setOrderId(createBean.getOrderId());
			bean.setUserId(createBean.getUserid());
			call.setData(bean);
			call.callService();	
			//退货状态：卖家发起   接受类型：买家
			verifyReturn(bean,call,20,1);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBuyerReturnInfoByOrderId_004() {
		Logger.start(true,"正例-获取买家订单的退单信息-订单为空");
		try {
			//创建一个订单
			TradingPlaceOrderCallService tradingService = new TradingPlaceOrderCallService();
			RefundCallService service = new RefundCallService();
			TradingServiceResp tradingServiceResp = tradingService
					.placeOrderWithLuxury(false);
			
			createBean=service.setCreateReturnBillBean(tradingServiceResp,tradingService);
			createCall=service.callCreateReturnBill(createBean,createCall);
            
			bean.setOrderId(0);
			bean.setUserId(createBean.getUserid());
			call.setData(bean);
			call.callService();	
			
			Logger.verifyEquals(true, call.getMsg1().contains("无效的orderid"), "验证无效的orderid");
			Logger.verifyEquals("400", call.getStatus(), "验证无效的Status");
			Logger.verifyEquals("12", call.getbCode(), "验证无效的bCode");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBuyerReturnInfoByOrderId_005() {
		Logger.start(true,"正例-获取买家订单的退单信息-userid为空");
		try {
			//创建一个订单
			TradingPlaceOrderCallService tradingService = new TradingPlaceOrderCallService();
			RefundCallService service = new RefundCallService();
			TradingServiceResp tradingServiceResp = tradingService
					.placeOrderWithLuxury(false);
			
			createBean=service.setCreateReturnBillBean(tradingServiceResp,tradingService);
			createCall=service.callCreateReturnBill(createBean,createCall);
            
			bean.setOrderId(createBean.getOrderId());
			bean.setUserId(99999999);
			call.setData(bean);
			call.callService();	
			List<ReturnBillInfoList> infoList=call.getCallData().getReturnBillInfoList();
			Logger.verifyEquals(0, infoList.size(), "验证ReturnBillInfoList为空");
			Logger.verifyEquals("", call.getMsg1(), "验证userId无效");
			Logger.verifyEquals("200", call.getStatus(), "验证无效的Status");
			Logger.verifyEquals("0", call.getbCode(), "验证无效的bCode");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private void verifyReturn(GetBuyerReturnInfoByOrderIdBean bean,GetBuyerReturnInfoByOrderIdCall call,int refundStatus,int accepterType)
	{
		List<ReturnBillInfoList> infoList=call.getCallData().getReturnBillInfoList();
		int count=0;
		for(int i=0;i<infoList.size();i++)
		{
		  count++;
		  CS_ReturnApplyBill returnApplyBill=returnApplyBillWapper.selectByApplyBillId(infoList.get(i).getReturnBillId()).get(0);
		  CS_ReturnApplyProducts returnproduct=returnApplyProducts.selectByApplyBillId(infoList.get(i).getReturnBillId()).get(0);
		  
		  Logger.verifyEquals(returnApplyBill.getReturnApplyBillId(), infoList.get(i).getReturnBillId(), "验证CS_ReturnApplyBill表第"+count+"条ReturnApplyBillId是否正确");
		
		  Logger.verifyEquals(returnproduct.getCatalogId(), infoList.get(i).getCatalogId(), "验证CS_ReturnApplyBill表第"+count+"条CatalogId是否正确");
		  
		  Logger.verifyEquals(returnproduct.getCatalogText(), infoList.get(i).getCatalogName(), "验证CS_ReturnApplyBill表第"+count+"条CatalogName是否正确");
		  Logger.verifyEquals(returnApplyBill.getReturnAmount().stripTrailingZeros().toPlainString(), infoList.get(i).getReturnAmount().stripTrailingZeros().toPlainString(), "验证CS_ReturnApplyBill表第"+count+"条ReturnAmount是否正确");
		  
		  Logger.verifyEquals(refundStatus, infoList.get(i).getReFundStatus(), "验证CS_ReturnApplyBill表第"+count+"条ReFundStatus是否正确");
		  Logger.verifyEquals(accepterType, infoList.get(i).getAccepterType(), "验证CS_ReturnApplyBill表第"+count+"条accepterType是否正确");
		}
	}
}